// pages/login/login.js
import { sendCode, userVerifyLogin } from '../../utils/getData.js';
const getopen = require('../../utils/maxutil.js');

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneinputValue:'', //
    password:'',
    countdown:'获取验证码', //倒计时
    wrong:false, //密码是否错误
    activityCoverPage:'', //局主题图片
    bjstart:'', //背景 触摸开始的坐标点
    bjnum:'', //背景 触摸结束的坐标点
    wxuserinfo:false, //微信信息是否授权
    disabledyzm:false, //验证码禁止点,
    avatarstatus:1,
    openId:wx.getStorageSync('openId'),
    fakeUserId:wx.getStorageSync('userId'),
    unionId:wx.getStorageSync('unionId'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(6666)
    this.setData({
      activityCoverPage:wx.getStorageSync('lookCoverPage'),
      wxuserinfo:app.globalData.wxuserinfo ? true :false,
    })

  },

  bindKeyInput: function (e) {
    
    this.setData({

      
      phoneinputValue: e.detail.value
    })
  },
  bindcode: function (e) {
    let than = this;
    this.setData({
      password: e.detail.value
    })

    console.log(e.detail.value,this.data.phoneinputValue);

    if(this.data.phoneinputValue && e.detail.value.length == 6){

      var openId=wx.getStorageSync('openId');
      
      //判断缓存内有没有openid
      if(wx.getStorageSync('openId') && wx.getStorageSync('openId') != "undefined"){
        

        this.getUserVerifyLogin();
      }else{
      //没有openid就再获取一次
        getopen.loginOpenid().then(res => {
          if(res.result.openId){
            than.getUserVerifyLogin();
          }
        })
      }
    }
  },
  // 获取验证码点击事件
  getyzm(){
    const { countdown, phoneinputValue } = this.data;
    let than = this;
    let timer_num = 60;
    let pattern = /^[1]([3-9])[0-9]{9}$/;
    if(!pattern.test(phoneinputValue)){
      wx.showToast({
		  	title: '请输入正确的手机号',
		  	icon: 'none',
		  	duration: 2000
		  })
    }
    if(countdown == '获取验证码' && pattern.test(phoneinputValue)){
      than.setData({
        disabledyzm:true
      },()=>{
        than.getSendCode();
      })
      let timeClock=setInterval(function(){
          timer_num--;
          than.setData({
            countdown:timer_num+'s'
          })
          if (timer_num == 0) {
              clearInterval(timeClock);
              than.setData({
                countdown:'获取验证码',
                disabledyzm:false
              })
          } 
      },1000)
    }
  },

 //获取验证码
 getSendCode(){
  let than = this;
  let data = {
    phoneNum:than.data.phoneinputValue
  }
  sendCode(data).then(res=> {
    console.log(res)
  })
  // request({
  //   url: "v1/pub/sms/sendCode",
  //   data,
  //   type:'GET',\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\  //   success(res) {
  //   }
  // })
},
//用户手机验证登录
getUserVerifyLogin(){
  let than = this;
  const { phoneinputValue, password } = this.data

  console.log("登录");
  let data = {
    phoneNumber:phoneinputValue,
    code:password,
    openId:wx.getStorageSync('openId'),
    fakeUserId:wx.getStorageSync('userId'),
    unionId:wx.getStorageSync('unionId'),
    weixinAvatarPath:app.globalData.wxuserinfo ? app.globalData.wxuserinfo.avatarUrl:'',
    weixinNickName:app.globalData.wxuserinfo ? app.globalData.wxuserinfo.nickName:'',
  }
  userVerifyLogin(data).then(res => {
      if(res.success){
        wx.setStorageSync('userId', res.result.userId);
        wx.setStorageSync('status',res.result.status) //0为需要登
        data.avatarstatus = wx.getStorageSync('avatarstatus')
        console.log("77777777",data.avatarstatus)
        if(res.result.status < 1 && data.avatarstatus != 5){
          //新用户上传头像
          wx.redirectTo({
            url: '../releaseAvatar/releaseAvatar',
          })
          return false;
        }else{
          if(res.result.status < 1 &&wx.getStorageSync('avatarstatus') == 5){
            wx.setStorageSync('status', 9)
          }
          wx.navigateBack({
            delta: 1
          })
          
        }
        
      }else{
            wx.showToast({
              title: res.message,
              icon: 'none',
              duration: 2000
            })

        than.setData({
          wrong:true
        })

        setTimeout(() => {
          than.setData({
            wrong:false})
        }, 1000);

      }
  })
},
// 获取微信信息，隐藏
getUserInfo: function(e) {
  console.log('获取微信信息')
  console.log(e.detail.userInfo)
  app.globalData.wxuserinfo = e.detail.userInfo;
  this.setData({
    wxuserinfo:true,
  })
},

  //背景 手指触摸动作开始 记录起点X坐标
  touchstartjubj: function(e){
    this.bjstart = e.changedTouches[0].clientX
  },
  //背景 手指离开
  touchendjubj: function(e){
    this.bjnum = e.changedTouches[0].clientX
    let number = this.bjnum - this.bjstart;
    if(number > 10){
      // 离开
    let pages = getCurrentPages()
      if(pages.length >= 0){
        wx.navigateBack({
          delta: 2
        })
      }else{
        wx.navigateBack({
          delta: 1
        })
      }
      // wx.navigateTo({
      //   url: '../subBureauDetails/subBureauDetails'
      // })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})